package com.example.app.ui.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.app.viewmodel.ArticleViewModel
import com.example.module.webview.WebView
import com.example.module.webview.rememberWebViewState
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun ArticleDetailScreen(articleContent: ArticleViewModel = viewModel(), onBack: () -> Unit = {}) {
    val webViewState = rememberWebViewState(data = articleContent.content)
    var fontScale by remember {
        mutableFloatStateOf(1.0f)
    }
    val scaffoldState = rememberBottomSheetScaffoldState()
    val coroutineScope = rememberCoroutineScope()
    BottomSheetScaffold(
        scaffoldState = scaffoldState,
        sheetPeekHeight = 0.dp,
        topBar = {
            TopAppBar(title = {
                Text(
                    text = "详情页",
                    fontSize = 18.sp,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxWidth()
                )
            }, navigationIcon = {
                Icon(imageVector = Icons.AutoMirrored.Filled.KeyboardArrowLeft,
                    contentDescription = null,
                    modifier = Modifier
                        .clickable { onBack() }
                        .padding(2.dp)
                        .size(32.dp))
            }, actions = {
                Icon(imageVector = Icons.Default.Settings,
                    contentDescription = "点击设置文字大小",
                    modifier = Modifier
                        .clickable {
                            coroutineScope.launch {
                                if (scaffoldState.bottomSheetState.currentValue == SheetValue.Expanded) {
                                    scaffoldState.bottomSheetState.partialExpand()
                                } else {
                                    scaffoldState.bottomSheetState.expand()
                                }
                            }
                        }
                        .padding(8.dp))
            })
        }, sheetContent = {
            Column(
                modifier = Modifier.padding(
                    top = 0.dp,
                    start = 12.dp,
                    end = 12.dp,
                    bottom = 26.dp
                )
            ) {
                Text(text = "字体大小", fontSize = 16.sp)
                Slider(value = fontScale, onValueChange = {
                    fontScale = it
                    webViewState.evaluateJavascript("document.body.style.zoom=$it")
                }, steps = 3, valueRange = 0.75f..1.75f)
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(text = "较小", fontSize = 14.sp, color = Color(0xFF999999)) //0.75
                    Text(text = "标准", fontSize = 14.sp, color = Color(0xFF999999)) //1.0
                    Text(text = "较大", fontSize = 14.sp, color = Color(0xFF999999)) //1.25
                    Text(text = "超大", fontSize = 14.sp, color = Color(0xFF999999)) //1.5
                    Text(text = "特大", fontSize = 14.sp, color = Color(0xFF999999)) //1.75
                }
            }
        }
    ) {
        LazyColumn(modifier = Modifier.padding(it)) {
            item {
                WebView(webViewState)
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun ArticleDetailScreenPreview() {
    ArticleDetailScreen()
}